﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class DonDH_NguyenVatLieuDAO
    {
        public List<DonDH_NguyenVatLieuDTO> LayDanhSachDonDH_NguyenVatLieu()
        {
            List<DonDH_NguyenVatLieuDTO> listDDHNVL = new List<DonDH_NguyenVatLieuDTO>();
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                String sql = "Select a.*, b.Ten from DonDH_NguyenVatLieu a, NhaCungCap b WHERE a.MaNCC = b.MaNCC";

                conn = DataProvider.ConnectDB();
                SqlCommand cm = new SqlCommand(sql, conn);
                reader = cm.ExecuteReader();
                while (reader.Read())
                {
                    DonDH_NguyenVatLieuDTO ddhnvlDTO = new DonDH_NguyenVatLieuDTO();
                    ddhnvlDTO.MaDDH = reader.GetInt32(0);
                    ddhnvlDTO.NgayLap = reader.GetDateTime(1);
                    ddhnvlDTO.DiaDiemGiaoHang = reader.GetString(2);
                    ddhnvlDTO.ThoiGianGiaohang = reader.GetDateTime(3);
                    ddhnvlDTO.MaNCC = reader.GetInt32(4);
                    ddhnvlDTO.Ten = reader.GetString(5);
                    listDDHNVL.Add(ddhnvlDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return listDDHNVL;
        }

        public int ThemDonDH_NguyenVatLieu(DonDH_NguyenVatLieuDTO ddhnvlDTO)
        {
            //String mess = "";
            int ID = -1;
            SqlConnection conn = null;
            try
            {
                conn = DataProvider.ConnectDB();
                String sql = "INSERT INTO DonDH_NguyenVatLieu(NgayLap,DiaDiemGiaoHang,ThoiGianGiaohang,MaNCC)";
                sql += "VALUES(@NgayLap,@DiaDiemGiaoHang,@ThoiGianGiaohang,@MaNCC)";
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                SqlParameter para;
                para = cmd.Parameters.Add("@NgayLap", SqlDbType.Date);
                para.Value = ddhnvlDTO.NgayLap;
                para = cmd.Parameters.Add("@DiaDiemGiaoHang", SqlDbType.NVarChar);
                para.Value = ddhnvlDTO.DiaDiemGiaoHang;
                para = cmd.Parameters.Add("@ThoiGianGiaohang", SqlDbType.Date);
                para.Value = ddhnvlDTO.ThoiGianGiaohang;
                para = cmd.Parameters.Add("@MaNCC", SqlDbType.Int);
                para.Value = ddhnvlDTO.MaNCC;
                cmd.ExecuteNonQuery();

                String strSQL = "Select @@IDENTITY";
                cmd = new SqlCommand(strSQL, conn);
                ID = Convert.ToInt32(cmd.ExecuteScalar());
                conn.Close();
            }
            catch (Exception ex)
            {
                //mess = "Lỗi thêm hóa đơn";
                String e = ex.ToString();
            }
            return ID;
        }
    }
}
